package org.corey.share.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.corey.share.db.entity.ArticleCatogory;
import org.corey.share.db.service.ArticleCatogoryService;
import org.corey.share.db.service.ArticleCatogoryServiceImpl;
import org.corey.share.db.service.AuthService;
import org.corey.share.db.service.AuthServiceImpl;

import com.opensymphony.xwork2.ActionSupport;

public class ArticleCatogoryAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private ArticleCatogoryService service = new ArticleCatogoryServiceImpl();
	private AuthService authService = AuthServiceImpl.getInstance();
	public Long[] deleteCatogoriesId;
	public ArticleCatogory saveOrUpdateCatogory;
	public List<ArticleCatogory> catogories = new ArrayList<ArticleCatogory>();

	public Long[] getDeleteCatogoriesId() {
		return deleteCatogoriesId;
	}

	public void setDeleteCatogoriesId(Long[] deleteCatogoriesId) {
		this.deleteCatogoriesId = deleteCatogoriesId;
	}

	public ArticleCatogory getSaveOrUpdateCatogory() {
		return saveOrUpdateCatogory;
	}

	public void setSaveOrUpdateCatogory(ArticleCatogory saveOrUpdateCatogory) {
		this.saveOrUpdateCatogory = saveOrUpdateCatogory;
	}

	public List<ArticleCatogory> getCatogories() {
		return catogories;
	}

	public void setCatogories(List<ArticleCatogory> catogories) {
		this.catogories = catogories;
	}

	/******************* Action接口 ***********************************/
	public String delete() {
		for (int i = 0; i < deleteCatogoriesId.length; i++) {
			service.deleteArticleCatogory(deleteCatogoriesId[i]);
		}
		return SUCCESS;
	}

	public String manage() {
		Long user = authService.getUserId();
		this.catogories = service.getAllArticleCatogory(user);
		return SUCCESS;
	}

	public String add() {
		saveOrUpdateCatogory.setUser(authService.getUserId());
		saveOrUpdateCatogory.setWriteDate(new Date());
		saveOrUpdateCatogory.setEditedDate(new Date());
		saveOrUpdateCatogory.setArticleCount(0);
		boolean isSuccess = service.addArticleCatogory(saveOrUpdateCatogory);
		if (isSuccess) {
			return SUCCESS;
		}
		return INPUT;
	}

	public String update() {
		boolean isSuccess = service.modifyArticleCatogory(saveOrUpdateCatogory);
		if (isSuccess) {
			return SUCCESS;
		}
		return INPUT;
	}
}
